# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=pygobject

pkgname=("${MINGW_PACKAGE_PREFIX}-python2-gobject2" "${MINGW_PACKAGE_PREFIX}-python3-gobject2" "${MINGW_PACKAGE_PREFIX}-pygobject2-devel")
pkgver=2.28.6
pkgrel=2
pkgdesc="Allows you to load glade interface files in a program at runtime (mingw-w64)"
arch=(any)
url="http://www.pygtk.org"
license=('LGPL')
groups=("${MINGW_PACKAGE_PREFIX}")
makedepends=("${MINGW_PACKAGE_PREFIX}-python2" "${MINGW_PACKAGE_PREFIX}-python3" "${MINGW_PACKAGE_PREFIX}-gobject-introspection" "${MINGW_PACKAGE_PREFIX}-gnome-common")
options=('staticlibs' 'strip')
source=(http://ftp.gnome.org/pub/gnome/sources/pygobject/${pkgver%.*}/${_realname}-${pkgver}.tar.xz
    win32-fix.patch
    python-m4.patch)
md5sums=('9415cb7f2b3a847f2310ccea258b101e'
         '5dcee45159fb9d08248d56d0e73849c7'
         '5391d75a08c19e975679a0385c319f79')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p1 -i ${srcdir}/win32-fix.patch
  patch -p1 -i ${srcdir}/python-m4.patch
  WANT_AUTOMAKE=latest autoreconf -fi
  #WANT_AUTOMAKE=latest ./autogen.sh
}

build() {
  cd "$srcdir"

  rm -rf python{2,3}-build
  mkdir python{2,3}-build devel
  CFLAGS+=" -D__USE_MINGW_ANSI_STDIO=1 -Wno-error=format"
  CXXFLAGS+=" -D__USE_MINGW_ANSI_STDIO=1"
  CPPFLAGS+=" -D__USE_MINGW_ANSI_STDIO=1"
  for builddir in python{2,3}-build; do
    pushd $builddir
    PYTHON=${MINGW_PREFIX}/bin/${builddir%-build} \
      ../${_realname}-$pkgver/configure --prefix=${MINGW_PREFIX} \
        --build=${MINGW_CHOST} \
        --host=${MINGW_CHOST} \
        --target=${MINGW_CHOST} \
        --disable-silent-rules \
        --disable-introspection
      make VERBOSE=1
    popd
  done
}

check() {
  for builddir in python{2,3}-build; do
    pushd ${srcdir}/$builddir
    PYTHON=${MINGW_PREFIX}/bin/${builddir%-build} \
      make check
    popd
  done
}

package_python2-gobject2() {
  pkgdesc="Python 2 bindings for GObject2 (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-glib2" "${MINGW_PACKAGE_PREFIX}-libffi" "${MINGW_PACKAGE_PREFIX}-gobject-introspection" "${MINGW_PACKAGE_PREFIX}-pygobject2-devel=$pkgver")

  cd python2-build
  make DESTDIR="$pkgdir" install
  
  find "${pkgdir}${MINGW_PREFIX}"/share/pygobject -name '*.py' -exec \
  sed -i '1s|#![ ]*/usr/bin/env python$|#!/usr/bin/env python2|' {} +

  rm -r "${pkgdir}${MINGW_PREFIX}"/share/gtk-doc

  # Split devel stuff
  cd ${srcdir}
  mkdir -p devel${MINGW_PREFIX}/{bin,lib,share/pygobject}
  mv "${pkgdir}${MINGW_PREFIX}"/bin/pygobject-codegen-2.0 devel${MINGW_PREFIX}/bin/
  mv {"$pkgdir",devel}${MINGW_PREFIX}/include
  mv {"$pkgdir",devel}${MINGW_PREFIX}/lib/pkgconfig
  mv {"$pkgdir",devel}${MINGW_PREFIX}/share/pygobject/xsl
}

package_python3-gobject2() {
  pkgdesc="Python 3 bindings for GObject2 (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-glib2" "${MINGW_PACKAGE_PREFIX}-libffi" "${MINGW_PACKAGE_PREFIX}-gobject-introspection" "${MINGW_PACKAGE_PREFIX}-pygobject2-devel=$pkgver")
  
  cd python3-build
  make DESTDIR="$pkgdir" install
  
  # Delete devel stuff
  rm -f "${pkgdir}${MINGW_PREFIX}"/bin/pygobject-codegen-2.0
  rm -r "${pkgdir}${MINGW_PREFIX}"/{include,lib/pkgconfig,share/{gtk-doc,pygobject}}
}

package_pygobject2-devel() {
  pkgdesc="Development files for the pygobject bindings"
  mv ${srcdir}/devel${MINGW_PREFIX} "${pkgdir}/"
}

package_mingw-w64-i686-pygobject2-devel() {
  package_pygobject2-devel
}

package_mingw-w64-x86_64-pygobject2-devel() {
  package_pygobject2-devel
}

package_mingw-w64-i686-python2-gobject2() {
  package_python2-gobject2
}

package_mingw-w64-i686-python3-gobject2() {
  package_python3-gobject2
}

package_mingw-w64-x86_64-python2-gobject2() {
  package_python2-gobject2
}

package_mingw-w64-x86_64-python3-gobject2() {
  package_python3-gobject2
}
